package com.zt.ftphelper;

import com.zt.ftphelper.model.ServerItem;
import com.zt.ftphelper.model.TaskItem;
import com.zt.ftphelper.util.Tools;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.io.*;
import java.util.Properties;

/**
 * @author zt
 * @Description:配置类
 * @date 19/08/2019 12:22
 */
public class Config {


    private Properties properties;

    /**
     * 初始目录地址
     */
    public static  String BASE_PATH;

    public static String FLASHFXP_LOCATION;

    public static String CONFIG = "/config.properties";


    public static final int DB_CLICK = 2;

    /**
     * 代表上級目录
     */
    public static final String  PARENT_DIR = "..";



    public static final String  DEFAULT_CHARSET = "UTF-8";


    public static final String  TYPE_DIR = "d";



    /**
     * 加载数据，如果文件不存在返回null。
     */
    public static final Properties load(String file) {

        String programPath  = new File(Config.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getParent();
        File config = new File(programPath+File.separator+"conf"+CONFIG);
        System.out.println("config="+config);
        Properties properties = null;
        if(config.exists()){
            try {
                InputStream  input = new FileInputStream(config);
                properties = new Properties();
                properties.load(input);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            try(
                    InputStreamReader input = new InputStreamReader(Config.class.getResourceAsStream(file), DEFAULT_CHARSET)) {
                properties = new Properties();
                properties.load(input);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return properties;
    }

    public String getString(String key) {
        return this.properties.getProperty(key);
    }

    private Boolean getBoolean(String key) {
        return  Boolean.valueOf(getString(key));
    }

    private Integer getInteger(String key) {
        return  Integer.valueOf(getString(key));
    }

    public static void store(){
        	try {

                String programPath  = new File(Config.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getParent();
                File configPath = new File(programPath+File.separator+"conf");
                if(!configPath.exists()){
                    configPath.mkdir();
                }
                File config = new File(configPath.getAbsolutePath()+CONFIG);
                if(!config.exists()){
                    config.createNewFile();
                }
                FileOutputStream oFile = new FileOutputStream(config, false);
                Config.getInstance().saveServers2Property();
                Config.getInstance().properties.store(new OutputStreamWriter(oFile, DEFAULT_CHARSET),"");
                oFile.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




    /**
     * taskTable视图展示用的文件列表
     */
    private ObservableList<TaskItem> taskData = FXCollections.observableArrayList();

    /**
     * serverTable视图展示用的文件列表
     */
    private ObservableList<ServerItem> savedServerData = FXCollections.observableArrayList();

    private Config() {
        this.properties = load(CONFIG);
        BASE_PATH = getString("defaultBasePath");
        FLASHFXP_LOCATION = getString("flashFxpLocation");
        Integer serverCount = getInteger("ftp.server.count");

        for(int i=1;i<=serverCount;i++){
            ServerItem serverItem = Tools.getServerItemFromString(i,getString("ftp.server."+i));
            savedServerData.add(serverItem);
        }

    }

    /**
     * 饿汉模式单例
     */
    private static final Config single = new Config();

    public static Config getInstance() {
        return single;
    }





    public ObservableList<TaskItem> getTaskData() {
        return taskData;
    }

    public void setTaskData(ObservableList<TaskItem> taskData) {
        this.taskData = taskData;
    }

    public ObservableList<ServerItem> getServerData() {
        return savedServerData;
    }

    public void setServerData(ObservableList<ServerItem> serverData) {
        this.savedServerData = serverData;
    }

    public void addString(String key, String value){
        this.properties.setProperty(key,value);
    }

    private void saveServers2Property() {
        this.properties.setProperty("ftp.server.count", String.valueOf(savedServerData.size()));
        for (int i = 1; i <= savedServerData.size(); i++) {
            String server =savedServerData.get(i-1).toString();

            addString("ftp.server."+i,server);

        }
    }
}
